using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Deepcode.Flux.Core.Data;

namespace Deepcode.Flux.Core.Systems.Scheduler
{
	/// <summary>
	/// Represents a task in the scheduled tasks table
	/// </summary>
	public class Task
	{
		// Private fields for storing record fields //
		private int				_PK_ID = -1;
		private string			_TaskName = "";
		private DateTime		_ExecuteNext = DateTime.Now;
		private bool			_Finished = false;
		private TaskFrequency	_Frequency = TaskFrequency.OnceOnly;
		private int				_Interval = 1;
		private	int				_TimesRemaining = -1;
		private string			_Execute = "";
		private bool			_TaskExpires = false;
		private DateTime		_ExpireTask = DateTime.Now;

		// Public fields exposing record fields //
		public int PK_ID { get { return _PK_ID; } set { _PK_ID = value; } }
		public string TaskName { get{ return _TaskName; } set{ _TaskName = value; }}
		public DateTime ExecuteNext{ get{ return _ExecuteNext; } set{ _ExecuteNext = value; }}
		public bool Finished{ get{ return _Finished; } set{ _Finished=value; }}
		public TaskFrequency Frequency{ get{ return _Frequency; } set{ _Frequency=value; }}
		public int Interval{ get{ return _Interval; } set{ _Interval = value; }}
		public int TimesRemaining{ get{ return _TimesRemaining; } set{ _TimesRemaining = value; }}
		public string Execute{ get{ return _Execute; } set{ _Execute = value; }}
		public bool TaskExpires{ get{ return _TaskExpires; } set{ _TaskExpires = value; }}
		public DateTime ExpireTask{ get{ return _ExpireTask; } set{ _ExpireTask = value; }}
		// Constructor to create new without loading
		public Task() { }

		// Constructor loading from a data row
		public Task(DataRow InitFromRow)
		{
			_PK_ID = SQLUtility.DBNull(InitFromRow["PK_ID"], -1);
			_TaskName = SQLUtility.DBNull(InitFromRow["TaskName"], "");
			_ExecuteNext = SQLUtility.DBNull(InitFromRow["ExecuteNext"], DateTime.Now);
			_Finished = SQLUtility.DBNull(InitFromRow["Finished"], false);
			_Frequency = (TaskFrequency) SQLUtility.DBNull( InitFromRow["Frequency"], -1 );
			_Interval = SQLUtility.DBNull( InitFromRow["Interval"], 1 );
			_TimesRemaining = SQLUtility.DBNull( InitFromRow["TimesRemaining"], -1);
			_Execute = SQLUtility.DBNull( InitFromRow["Execute"], "" );
			_TaskExpires = SQLUtility.DBNull( InitFromRow["TaskExpires"], false );
			_ExpireTask = SQLUtility.DBNull( InitFromRow["ExpireTask"], DateTime.Now );
		}
	}
}
